package jgl.vajra.operator.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;

import jgl.vajra.operator.R;
import jgl.vajra.operator.databinding.ViewVerifyCodeBinding;
import jgl.vajra.operator.listener.VerifyImageCodeListener;
import jgl.vajra.operator.widget.base.BaseCustomView;
import jgl.vajra.operator.widget.interfaces.VerifyCodeListener;
import jgl.vajra.operator.widget.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/8/6.
 */

public class VerifyCodeView extends BaseCustomView<ViewVerifyCodeBinding> implements VerifyImageCodeListener {

    private int codeViewBackground;
    private String codeViewText;
    private List<VerifyCodeListener> mVerifyCodeListenerList;

    public VerifyCodeView(Context context) {
        super(context);
    }

    public VerifyCodeView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void initData(Context context) {
        mVerifyCodeListenerList = new ArrayList<>();
    }

    @Override
    public void initEvent(Context context) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.view_verify_code;
    }

    @Override
    protected void initView(Context context, AttributeSet attrs) {
       /* TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.VerifyCodeView);
        //  codeViewBackground = typedArray.getResourceId(R.styleable.VerifyCodeView_codeViewBackground, R.drawable.shape_redb9_4dp);
        codeViewText = typedArray.getString(R.styleable.VerifyCodeView_codeViewText);
        if (TextUtils.isEmpty(codeViewText))
            codeViewText = "发送验证码";
        float codeViewTextSize = typedArray.getDimension(R.styleable.VerifyCodeView_codeViewTextSize, R.dimen.sp_12);
        mBinding.tvVerifyCode.setTextSize(TypedValue.COMPLEX_UNIT_PX, codeViewTextSize);
        //mBinding.tvVerifyCode.setBackgroundResource(codeViewBackground);
        mBinding.tvVerifyCode.setText(codeViewText);
        typedArray.recycle();*/
    }

    public void setVerifyCodeListener(VerifyCodeListener listener) {
        this.mVerifyCodeListenerList.add(listener);
    }

    boolean isVerifyCode;

    public void getVerifyCode(String phone) {
        if (TextUtils.isEmpty(phone)) {
            ToastUtils.showShort("请输入电话号码");
            return;
        }


    }


/*
    private void getSmsThreshold(String phone) {
        RetrofitJsonManager.getInstance().getApiService().getSmsThreshold().compose(RxJavaHttpManager.<SmsThresholdBean>applyTransformer()).subscribe(new ProgressObserver<SmsThresholdBean>(null) {

            @Override
            public void _onNext(SmsThresholdBean o) {
                if (o.status == 1) {
                    smsThresholdBean = o;
                    if (verifyImageCodeDialogFragment == null)
                        verifyImageCodeDialogFragment = FragmentManager.getVerifyImageCodeDialogFragment();
                    verifyImageCodeDialogFragment.setPhone(phone);
                    if (!verifyImageCodeDialogFragment.isShowing()) {
                        verifyImageCodeDialogFragment.show(AppActivityManager.getAppActivityManager().currentActivity().getSupportFragmentManager());
                        verifyImageCodeDialogFragment.setVerifyImageCodeListener(VerifyCodeView.this);
                    }

                } else {
                    onVerifyImageCode(phone, "");
                }
            }

            @Override
            public void _onError(String errorMessage, int errorCode) {
                super._onError(errorMessage, errorCode);

            }
        });

    }
*/


    @Override
    public void onVerifyImageCode(String phone, String imageCode) {

        if (isVerifyCode) return;
        isVerifyCode = true;
/*
        RetrofitJsonManager.getInstance().getApiService().getVerifyCode(imageCode, phone).compose(RxJavaHttpManager.<VerifyCodeBean>applyTransformer()).subscribe(new ProgressObserver<VerifyCodeBean>(false,null) {
            @Override
            public void _onNext(VerifyCodeBean o) {
                if (mVerifyCodeListenerList.size() != 0) {
                    for (VerifyCodeListener listener :
                            mVerifyCodeListenerList) {
                        listener.success(o);
                    }

                }
                TimerUtils.countdown(60).subscribe(new CommonObserver<Integer>() {
                    @Override
                    public void onNext(Integer integer) {
                        super.onNext(integer);
                        mBinding.tvVerifyCode.setText("重新发送(" + integer + ")");
                        mBinding.tvVerifyCode.setEnabled(false);
                    }


                    @Override
                    public void onComplete() {
                        super.onComplete();
                        mBinding.tvVerifyCode.setEnabled(true);
                        mBinding.tvVerifyCode.setText("发送验证码");
                        isVerifyCode = false;
                        for (VerifyCodeListener listener :
                                mVerifyCodeListenerList) {
                            listener.complete();
                        }
                    }
                });
            }

            @Override
            public void _onError(String errorMessage, int errorCode) {
                super._onError(errorMessage, errorCode);
                isVerifyCode = false;
                for (VerifyCodeListener listener :
                        mVerifyCodeListenerList) {
                    listener.fail(errorMessage, errorCode);
                }
            }
        });
*/
    }
}
